package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.dto.ScanDto;
import com.igeekhome.mapper.*;
import com.igeekhome.pojo.DNetwork;
import com.igeekhome.pojo.Scan;
import com.igeekhome.service.IScanService;
import com.igeekhome.util.DtreeBean;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ScanServiceImpl
 * @Author admin(汪璇)
 * @Date 2020/2/11 11:25
 * @Description TODO
 */
@Service("ScanService")
public class ScanServiceImpl implements IScanService {
    //寄件站
    private Integer[] sendStation = {15, 27, 21, 19, 16};
    
    //中转站
    private Integer[] TransferStation = {17, 19, 16};
    
    //终点站
    private Integer[] Terminus = {17, 19, 18, 28};
    
    @Autowired
    private ScanDtoMapper scanDtoMapper;
    
    @Autowired
    private ScanMapper scanMapper;
    
    @Autowired
    private DNetworkMapper networkMapper;
    
    @Autowired
    private OrderListDtoMapper orderListMapper;
    
    
    /**
     * @Description 收件扫描列表
     * @Param [network, page, limit]
     * @Return com.igeekhome.util.TableResultBean 收件扫描表集合
     **/
    @Override
    public TableResultBean receiveScanList(DNetwork network, Integer page, Integer limit) {
        ScanDto scanDto = returnScanDto(network, page, limit);
        scanDto.setScanType(15);
        List<ScanDto> scanDtos = scanDtoMapper.selectByAll(scanDto);
        
        long total = new PageInfo<ScanDto>(scanDtos).getTotal();
        return TableResultBean.ok(total, scanDtos);
    }
    
    /**
    * @Description 返回客服问题扫描数据
    * @Param [network, page, limit]
    * @Return com.igeekhome.util.TableResultBean
    **/
    @Override
    public TableResultBean proList(DNetwork network, Integer page, Integer limit) {
        ScanDto scanDto = returnScanDto(network, page, limit);
        scanDto.setScanType(22);
        List<ScanDto> scanDtos = scanDtoMapper.selectByAll(scanDto);
    
        long total = new PageInfo<ScanDto>(scanDtos).getTotal();
        return TableResultBean.ok(total, scanDtos);
    }
    
    /**
     * @Description 收件交单扫描列表
     * @Param [network, page, limit]
     * @Return com.igeekhome.util.TableResultBean 收件交单扫描表集合
     **/
    @Override
    public TableResultBean receiptDocScanList(DNetwork network, Integer page, Integer limit) {
        ScanDto scanDto = returnScanDto(network, page, limit);
        scanDto.setScanType(27);
        List<ScanDto> scanDtos = scanDtoMapper.selectByAll(scanDto);
        
        long total = new PageInfo<ScanDto>(scanDtos).getTotal();
        return TableResultBean.ok(total, scanDtos);
    }
    
    /**
     * @Description 收件交单操作
     * @Param [id]
     * @Return com.igeekhome.util.ResultBean
     **/
    @Override
    public ResultBean receiveScan(Integer id) {
        return scanOp(id);
    }
    
    //扫描单分配操作
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultBean ditScaner(Integer staffId, Integer[] scans) {
        // 设置回滚点
        Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
        try {
            for (Integer scan : scans) {
                Scan newscan = new Scan().setId(scan).setScannerId(staffId);
                scanMapper.updateByPrimaryKeySelective(newscan);
                Scan scan1 = scanMapper.selectByPrimaryKey(scan);
                Scan newScan = returnScanPower(scan1);
                scanMapper.updateByPrimaryKeySelective(newScan);
            }
            return ResultBean.ok();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
            return ResultBean.fail("数据提交失败,已回滚");
        }
    }
    
    /**
     * @Description 装袋扫描列表
     * @Param [network, page, limit]
     * @Return com.igeekhome.util.TableResultBean 收件交单扫描表集合
     **/
    @Override
    public TableResultBean bagScanList(DNetwork network, Integer page, Integer limit) {
        ScanDto scanDto = returnScanDto(network, page, limit);
        scanDto.setScanType(21);
        List<ScanDto> scanDtos = scanDtoMapper.selectByAll(scanDto);
        
        long total = new PageInfo<ScanDto>(scanDtos).getTotal();
        return TableResultBean.ok(total, scanDtos);
    }
    
    /**
     * @Description装袋扫描操作
     * @Param [id]
     * @Return com.igeekhome.util.ResultBean
     **/
    @Override
    public ResultBean bagScan(Integer id) {
        return scanOp(id);
    }
    
    
    /**
     * @Description 问题件扫描列表
     * @Param [network, page, limit]
     * @Return com.igeekhome.util.TableResultBean 问题件扫描表集合
     **/
    @Override
    public TableResultBean problemScanList(DNetwork network, Integer page, Integer limit) {
        ScanDto scanDto = returnScanDto(network, page, limit);
        scanDto.setScanType(19);
        List<ScanDto> scanDtos = scanDtoMapper.selectByAll(scanDto);
        
        long total = new PageInfo<ScanDto>(scanDtos).getTotal();
        return TableResultBean.ok(total, scanDtos);
    }
    
    /**
     * @Description问题件扫描操作
     * @Param [id]
     * @Return com.igeekhome.util.ResultBean
     **/
    @Override
    public ResultBean problemScan(Integer id) {
        return scanOp(id);
    }
    
    /**
     * @Description问题件设置操作
     * @Param [id]
     * @Return com.igeekhome.util.ResultBean
     **/
    @Override
    public ResultBean problemScanHandle(Integer id) {
        Scan scan = scanMapper.selectByPrimaryKey(id);
        Scan newScan = new Scan().setScanType(22);
        scanMapper.updateByPrimaryKeySelective(newScan);
        return ResultBean.success();
    }
    
    @Override
    public TableResultBean sendScanList(DNetwork network, Integer page, Integer limit) {
        ScanDto scanDto = returnScanDto(network, page, limit);
        scanDto.setScanType(16);
        List<ScanDto> scanDtos = scanDtoMapper.selectByAll(scanDto);
        
        long total = new PageInfo<ScanDto>(scanDtos).getTotal();
        return TableResultBean.ok(total, scanDtos);
    }
    
    /**
     * @Description发件扫描设置下一站网点
     * @Param [scanIds, nextStatus]
     * @Return com.igeekhome.util.ResultBean
     **/
    @Override
    public ResultBean sendScan(Integer[] scanIds, Integer nextStatus) {
        for (Integer scanId : scanIds) {
            Scan scan = scanMapper.selectByPrimaryKey(scanId);
            if (nextStatus.equals(scan.getDestinationNetworkId())) {
                Scan newscan = new Scan().setId(scanId).setScanType(17).setScanPower(3).setNextStopId(scan.getDestinationNetworkId());
                scanMapper.updateByPrimaryKeySelective(newscan);
                return ResultBean.success();
            } else {
                Scan newscan = new Scan().setId(scanId).setScanType(17).setScanPower(2).setNextStopId(scan.getDestinationNetworkId());
                scanMapper.updateByPrimaryKeySelective(newscan);
                return ResultBean.success();
            }
        }
        
        return null;
    }
    
    /**
     * @Description 发件扫描中返回下一站网点列表
     * @Param []
     * @Return com.igeekhome.util.ResultBean
     **/
    @Override
    public ResultBean networkList() {
        List<DNetwork> dNetworks = networkMapper.selectByExample(null);
        List<DtreeBean> dtreeNodes = new ArrayList<>();
        for (DNetwork dNetwork : dNetworks) {
            DtreeBean dtreeBean = DtreeBean.build(dNetwork.getNetworkId(), dNetwork.getNetworkName(), dNetwork.getParentNetworkId());
            if (dtreeBean.getParentId() == null) {
                dtreeBean.setParentId(0);
            }
            dtreeNodes.add(dtreeBean);
        }
        return ResultBean.ok(dtreeNodes);
    }
    
    /**
     * @Description到件扫描列表
     * @Param [network, page, limit]
     * @Return com.igeekhome.util.TableResultBean
     **/
    @Override
    public TableResultBean arriveScanList(DNetwork network, Integer page, Integer limit) {
        ScanDto scanDto = returnScanDto(network, page, limit);
        scanDto.setScanType(17);
        List<ScanDto> scanDtos = scanDtoMapper.selectByAll(scanDto);
        
        long total = new PageInfo<ScanDto>(scanDtos).getTotal();
        return TableResultBean.ok(total, scanDtos);
    }
    
    /**
     * @Description 到件扫描操作
     * @Param [id]
     * @Return com.igeekhome.util.ResultBean
     **/
    @Override
    public ResultBean arriveScan(Integer id) {
        return scanOp(id);
    }
    
    
    /**
    * @Description 派件扫描列表
    * @Param [network, page, limit]
    * @Return com.igeekhome.util.TableResultBean
    **/
    @Override
    public TableResultBean distributeScanList(DNetwork network, Integer page, Integer limit) {
        ScanDto scanDto = returnScanDto(network, page, limit);
        scanDto.setScanType(18);
        List<ScanDto> scanDtos = scanDtoMapper.selectByAll(scanDto);
        
        long total = new PageInfo<ScanDto>(scanDtos).getTotal();
        return TableResultBean.ok(total, scanDtos);
    }
    
    /**
    * @Description 派件扫描操作
    * @Param [id]
    * @Return com.igeekhome.util.ResultBean
    **/
    @Override
    public ResultBean distributeScan(Integer id) {
        return scanOp(id);
    }
    
    /**
    * @Description 派件交单扫描列表
    * @Param [network, page, limit]
    * @Return com.igeekhome.util.TableResultBean
    **/
    @Override
    public TableResultBean dispatchDocScanList(DNetwork network, Integer page, Integer limit) {
        ScanDto scanDto = returnScanDto(network, page, limit);
        scanDto.setScanType(28);
        List<ScanDto> scanDtos = scanDtoMapper.selectByAll(scanDto);
        long total = new PageInfo<ScanDto>(scanDtos).getTotal();
        return TableResultBean.ok(total, scanDtos);
    }
    
    /**
    * @Description 派件交单扫描操作
    * @Param [id]
    * @Return com.igeekhome.util.ResultBean
    **/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultBean dispatchDocScan(Integer id) {
        // 设置回滚点
        Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
        try {
            Scan newscan = new Scan().setScanType(29).setId(id);
            System.out.println(id);
            scanMapper.updateByPrimaryKeySelective(newscan);
            Scan scan = scanMapper.selectByPrimaryKey(id);
            System.out.println(scan.getWaybillNumber());
            orderListMapper.updateByWaybillId(scan.getWaybillNumber(),"2");
            return ResultBean.success();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
            return ResultBean.fail("数据提交失败,已回滚");
        }
    }
    
    
    /**
     * @Description 返回一个搜索条件和开启分页(抽取出来的通用代码)
     * @Param [network, page, limit]
     * @Return com.igeekhome.dto.ScanDto
     **/
    public ScanDto returnScanDto(DNetwork network, Integer page, Integer limit) {
        //开启分页
        PageHelper.startPage(page, limit);
        ScanDto scanDto = new ScanDto().setAgentId(network.getNetworkId());
        return scanDto;
    }
    
    
    /**
     * @Description这是一个判断扫描表处于什么站点并返回其下一个权限的方法
     * @Param [scan]
     * @Return com.igeekhome.pojo.Scan这是一个用于条件更新的Scan
     **/
    public Scan returnScanPower(Scan scan) {
        if (scan.getScanPower() == 1) {
            Scan newScan = new Scan();
            for (int i = 0; i < sendStation.length; i++) {
                if (sendStation[i].equals(scan.getScanType())) {
                    newScan.setScanType(sendStation[i + 1]).setId(scan.getId());
                    return newScan;
                }
            }
        } else if (scan.getScanPower() == 2) {
            Scan newScan = new Scan();
            for (int i = 0; i < TransferStation.length; i++) {
                if (TransferStation[i].equals(scan.getScanType())) {
                    newScan.setScanType(TransferStation[i + 1]).setId(scan.getId());
                    return newScan;
                }
            }
        } else if (scan.getScanPower() == 3) {
            Scan newScan = new Scan();
            for (int i = 0; i < Terminus.length; i++) {
                if (Terminus[i].equals(scan.getScanType())) {
                    newScan.setScanType(Terminus[i + 1]).setId(scan.getId());
                    return newScan;
                }
            }
        }
        return null;
    }
    
    /**
     * @Description简单扫描的重复操作抽取
     * @Param [id]
     * @Return com.igeekhome.util.ResultBean
     **/
    public ResultBean scanOp(Integer id) {
        Scan scan = scanMapper.selectByPrimaryKey(id);
        Scan newScan = returnScanPower(scan);
        scanMapper.updateByPrimaryKeySelective(newScan);
        return ResultBean.success();
    }
    
    
}
